草庐IT

根据命名约定返回 Boolean 类的 getter 的 Java 名称

全部标签

ruby - 是否有类似 String#scan 的函数,但返回 MatchDatas 数组?

我需要一个函数来返回字符串中正则表达式的所有匹配项和找到匹配项的位置(我想突出显示字符串中的匹配项)。有一个String#match返回MatchData,但只针对第一个匹配项。有没有比类似的方法更好的方法matches=[]beginmatch=str.match(regexp)breakunlessmatchmatches 最佳答案 如果您只需要遍历MatchData对象,您可以在扫描block中使用Regexp.last_match,例如:string.scan(regex)domatch_data=Regexp.last_m

ruby - 工头开始返回未找到的包

我最近从使用Ubuntu系统Ruby切换到使用RVM。当我运行foremanstart时,无论我的Procfile中的命令是什么,我都会收到一个未找到的错误。我当前的Procfile是:web:bundleexecunicorn-p$PORT-c./unicorn.rb所以错误是:/home/timmillwood/.rvm/gems/ruby-1.9.3-p327/gems/foreman-0.60.2/bin/foreman-runner:41:exec:bundle:notfound哪个工头返回/home/timmillwood/.rvm/gems/ruby-1.9.3-p327

ruby-on-rails - Rails 中带后备功能的动态命名空间 Controller

我对新的Rails应用程序有一个有点奇怪的要求。我需要构建一个应用程序,其中所有路由都在多个命名空间中定义(让我解释一下)。我想要一个应用程序,其中学校科目(数学、英语等)是namespace:%w[mathenglish].eachdo|subject|namespacesubject.to_symdoresources:studentsendend这很棒而且有效,但它需要我为每个主题创建一个命名空间StudentsController,这意味着如果我添加一个新主题,那么我需要创建一个新Controller。我想创建一个Base::StudentsController,如果Math:

ruby-on-rails - Rails 如何在渲染 json 时更改属性名称?

在我的Controller中我有:@pakkes=Pakke.where("navnlike?","%#{params[:q]}%")respond_todo|format|format.html#index.html.erbformat.xml{render:xml=>@pakkes}format.json{render:json=>@pakkes.map(&:attributes)}end如何在渲染JSON时将属性navn更改为name? 最佳答案 您可以使用Pakke中的一行方法完成此操作:defas_json(*args)s

ruby - 如何在 selenium-webdriver 中获取窗口标题、ID 和名称?

我正在尝试从selenium-webdriver(ruby)实现以下方法get_all_window_idsget_all_window_titlesget_all_window_names我运行了SeleniumIDE并将我的脚本导出到RubyTest::Unit。另存为.rb使用AptanaStudio3打开我的脚本进行编辑初始代码片段如下:require"rubygems"require"selenium-webdriver"require"test/unit"classSwitchToPopup3我不断得到的错误是NoMethodError:undefinedmethod`ge

Ruby:Ruby 中优雅的数组初始化和返回

我有一个方法:defdeltas_to_board_locations(deltas,x,y)board_coords=[]deltas.each_slice(2)do|slice|board_coords其中deltas是一个数组,x,y是fixnums。有没有办法去掉第一行和最后一行,让方法更优雅?喜欢:defdeltas_to_board_locations(deltas,x,y)deltas.each_slice(2)do|slice|board_coords 最佳答案 deltas.each_slice(2).flat_m

ruby - Ruby中Base的元类和Derived类的元类是什么关系?

在Ruby中,我们可以在单例方法中使用super来调用对应父类(superclass)的单例方法,如下面的代码所示。classBasedefself.class_methodputs"Baseclassmethod"endendclassDerived但是,我似乎不太明白Derived.class_method中对super的调用如何到达Base.class_method。我假设class_method是在他们的元类上定义的,这是否意味着他们的元类具有父/子关系?(我无法通过实验完全证实这一点)更新:我问这个问题是因为我记得在某处看到基类和派生类的元类之间存在某种关系(但我找不到它不再

ruby - 如何在 ruby​​ (1.9) case 语句中对返回值进行多重赋值?

这样做效果很好:q=caseperiod_groupwhen'day'then[7,'D']when'week'then[7,'WW']else['12','MM']endlimit,pattern=q[0],q[1]但我的第一次尝试:limit,pattern=caseperiod_groupwhen'day'then7,'D'when'week'then7,'WW'else'12','MM'end以语法错误结束:syntaxerror,unexpected',',expectingkeyword_endwhen'day'then7,'D'我错过了什么吗?

ruby - rspec 测试 ruby​​ 方法是否返回数组

我有一个返回数组的方法。我需要使用rspec对其进行测试。有没有我们可以测试的方法:defget_ids####returnsarrayofidsendsubject.get_ids.shouldbe_array或result=subject.get_idsresult.shouldbean_instance_of(Array) 最佳答案 好吧,这取决于您要查找的内容。检查返回值是否为数组(be_an_instance_of):expect(subject.get_ids).tobe_an_instance_of(Array)或者检

ruby-on-rails - 在 Rails 中,我可以在它返回之前在 Action 中访问 response.body 吗?

在Rails中,我可以在action返回之前访问response.body吗?假设我想在它返回之前做一些最终的字符串替换,我可以访问response.body,即View返回的响应吗? 最佳答案 在你的Controller中尝试after_filter。您应该可以从那里编辑您的response.body。对我来说,我需要删除xml中的一些ASCII字符,因此我这样做了。after_filter:sanitize_xmldefsanitize_xml#cleantheresponsebodybyaccessingresponse.bo